package ldh.im.fxbase.data.pojo;

import ldh.im.fxbase.cache.SoftId;
import ldh.im.fxbase.data.pojo.status.ImSessionStatus;
import ldh.im.fxbase.data.pojo.status.ImSessionType;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 消息会话
 */
public class ImSession implements SoftId {

    private Long id;
    private String currentUserId;
    private String name;
    private String fromUserId;
    private String toUserId;
    private String serverId;
    private ImSessionType sessionType;
    private String groupCode;
    private Integer hotMessageTotal;
    private String latelyMessage;
    private boolean isWaking = false;
    private boolean isTop = false;                //  是否置顶（发送方）
    private boolean isDisturb = false;            //  是否打扰模式（发送方）
    private boolean isWaking2 = false;
    private boolean isTop2 = false;               //  是否置顶（接收端）
    private boolean isDisturb2 = false;           //  是否打扰模式（接收端）
    private String ext1;
    private String ext2;
    private ImSessionStatus status;
    private Date createTime;
    private Date updateTime;

    private transient ImGroup imGroup;
    private transient ImUser toImUser;

    public ImSession() {
        sessionType = ImSessionType.chat;
        createTime = new Date();
        updateTime = createTime;
    }

    public static ImSession build(String userId, ImUser toImUser) {
        ImSession imSession = new ImSession();
        imSession.setCurrentUserId(userId);
        imSession.setFromUserId(userId);
        imSession.setToImUser(toImUser);
        if (toImUser != null) {
            imSession.setToUserId(toImUser.getUserId());
            imSession.setName(toImUser.getUserName());
        }
        imSession.setLatelyMessage("");
        imSession.setHotMessageTotal(0);
        imSession.setSessionType(ImSessionType.chat);
        imSession.setStatus(ImSessionStatus.created);
        return imSession;
    }

    public static ImSession build(String userId, ImGroup group) {
        ImSession imSession = build(userId, (ImUser) null);
        if (group != null) {
            imSession.setGroupCode(group.getServerId());
            imSession.setName(group.getName());
            imSession.setImGroup(group);
            imSession.setServerId(group.getSessionId());
            if (imSession.getServerId() != null) {
                imSession.setStatus(ImSessionStatus.enable);
            }
        }
        return imSession;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCurrentUserId() {
        return currentUserId;
    }

    public void setCurrentUserId(String currentUserId) {
        this.currentUserId = currentUserId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFromUserId() {
        return fromUserId;
    }

    public void setFromUserId(String fromUserId) {
        this.fromUserId = fromUserId;
    }

    public String getToUserId() {
        return toUserId;
    }

    public void setToUserId(String toUserId) {
        this.toUserId = toUserId;
    }

    public String getGroupCode() {
        return groupCode;
    }

    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public Integer getHotMessageTotal() {
        return hotMessageTotal;
    }

    public void setHotMessageTotal(Integer hotMessageTotal) {
        this.hotMessageTotal = hotMessageTotal;
    }

    public String getLatelyMessage() {
        return latelyMessage;
    }

    public void setLatelyMessage(String latelyMessage) {
        this.latelyMessage = latelyMessage;
    }

    public String getExt1() {
        return ext1;
    }

    public void setExt1(String ext1) {
        this.ext1 = ext1;
    }

    public String getExt2() {
        return ext2;
    }

    public void setExt2(String ext2) {
        this.ext2 = ext2;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public boolean isTop() {
        return isTop;
    }

    public void setTop(boolean top) {
        isTop = top;
    }

    public boolean isDisturb() {
        return isDisturb;
    }

    public void setDisturb(boolean disturb) {
        isDisturb = disturb;
    }

    public boolean isP2p() {
        return toUserId != null;
    }

    public ImSessionType getSessionType() {
        return sessionType;
    }

    public void setSessionType(ImSessionType sessionType) {
        this.sessionType = sessionType;
    }

    public boolean isTop2() {
        return isTop2;
    }

    public void setTop2(boolean top2) {
        isTop2 = top2;
    }

    public boolean isDisturb2() {
        return isDisturb2;
    }

    public void setDisturb2(boolean disturb2) {
        isDisturb2 = disturb2;
    }

    public boolean isWaking() {
        return isWaking;
    }

    public void setWaking(boolean waking) {
        isWaking = waking;
    }

    public boolean isWaking2() {
        return isWaking2;
    }

    public void setWaking2(boolean waking2) {
        isWaking2 = waking2;
    }

    public ImGroup getImGroup() {
        return imGroup;
    }

    public void setImGroup(ImGroup imGroup) {
        this.imGroup = imGroup;
    }

    public ImUser getToImUser() {
        return toImUser;
    }

    public void setToImUser(ImUser toImUser) {
        this.toImUser = toImUser;
    }

    public boolean isDisturb(String userId) {
        return (fromUserId.equals(userId) && isDisturb()) || (!fromUserId.equals(userId) && isDisturb2());
    }

    @Override
    public String getSoftId() {
        return id+"";
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ImSession)) return false;
        ImSession imSession = (ImSession) obj;
        return imSession.getId().equals(this.getId());
    }

    @Override
    public int hashCode() {
        return this.getId().hashCode();
    }

    public String getMessageSessionName() {
        if (toImUser != null) {
            return toImUser.getUserName();
        }
        if (imGroup != null) {
            return imGroup.getName();
        }
        if (this.getName() != null) {
            return getName();
        }
        return getFromUserId();
    }

    public ImSessionStatus getStatus() {
        return status;
    }

    public void setStatus(ImSessionStatus status) {
        this.status = status;
    }
}
